require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

require(['jquery', 'doT', 'datetimepicker', 'select2', 'common', 'bootstrap'], function($, doT, datetimepicker, select2, common) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var school_id = sessionStorage.getItem('school_id');

        $('#tableTrade').hide();

        function initDatetimepicker() {
            var date = new Date();
            date = new Date(date - 24 * 60 * 60 * 1000);
            var date_string = date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate();
            $('.datepicker').datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                maxView: 'decade',
                minView: 'month',
                endDate: date,
                initialDate: date
            });
            $('#tradedateStart').val(date_string);
            $('#tradedateEnd').val(date_string);          
        }
        initDatetimepicker();

        //获取表单的信息
        $('#tradeForm').submit(function() {

            var start_time = Date.parse($('#tradedateStart').val() + ' 00:00:00');
            var end_time = Date.parse($('#tradedateEnd').val() + ' 23:59:59');

            if (start_time >= end_time) {
                $('#tips').text('请选择正确的日期！');
                return false;
            } else {
                $('#tips').text('');
            }

            var data = {
                'start_time': start_time,
                'end_time': end_time
            };

            $.get(common.urlBase + '/transaction/trades/query/get', data, function(data, statu) {
                data = eval('(' + data + ')');
                if (data['ok']) {
                    $('#tableTrade').show();
                    var tradeList = data.data.obj;
                    var tradeTmpl = doT.template($('#template_trades').text());
                    $('#trades').html(tradeTmpl(tradeList));
                } else {
                    $('#tableTrade').hide();
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                    $('#trades').html('');
                }
            });
            return false;
        });
    });
});
